package com.baiyang.app.ui.showPage;

import android.animation.AnimatorSet;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.baiyang.app.R;

import java.util.ArrayList;
import java.util.List;

public class ShowPage extends RelativeLayout implements GestureDetector.OnGestureListener {
    // 三张页面
    private View page1,page2,page,showPage;
    // 文章内容
    private String str;
    // 页数
    private int pageNum;
    // 当前是第几页
    private int index = 1;
    private int showSize = 0;
    private int allSize = 0;
    private List<Integer> sizes;
    //定义手势检测器实例
    private GestureDetector detector;

    private ConstraintLayout top;
    private RelativeLayout content;
    private boolean show = false;
    public ShowPage(Context context) {
        super(context);
        init();
    }

    public ShowPage(Context context, AttributeSet attrs) {
        super(context, attrs);
        View root = LayoutInflater.from(context).inflate(R.layout.show_page, this, true);
        top = findViewById(R.id.top);
        content = findViewById(R.id.content);
        init();
    }


    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        top.setTranslationY(-top.getHeight());
    }

    public void setStr(String str){
        this.str = str;
        // 初始化第一页
        ReadTextView text = showPage.findViewById(R.id.read_text_view);
        text.setText(str);
        allSize = str.length();
        sizes = new ArrayList<>();
    }

    private void init(){
        //初始化手势检测器
        detector=new GestureDetector(content.getContext(), this);
        page1 = LayoutInflater.from(getContext()).inflate(R.layout.page,
                null);
        page2 = LayoutInflater.from(getContext()).inflate(R.layout.page,
                null);
        content.addView(page1, 0, new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        content.addView(page2, 0, new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        page = page2;
        showPage = page1;
        content.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return detector.onTouchEvent(event);
            }
        });
        top.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }



//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        // TODO Auto-generated method stub
//        //将触碰事件交给GestureDetector处理
//        return detector.onTouchEvent(event);
//    }

    /**
     * todo: return false时onFling方法无法触发
     * @param e
     * @return
     */
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        if(!show){
            ObjectAnimator.ofFloat(top, "translationY", -top.getHeight(),0).setDuration(100).start();
        }else{
            ObjectAnimator.ofFloat(top, "translationY", 0,-top.getHeight()).setDuration(100).start();
        }
        show = !show;
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //如果向右滑动的距离大于50mm
        if (e1.getX()-e2.getX()>50) {
            // 滑动之前的界面
            ReadTextView text = showPage.findViewById(R.id.read_text_view);
            if(showSize + text.getCharNum() == allSize){
                Toast.makeText(getContext(),"最后一页",Toast.LENGTH_SHORT).show();
                return false;
            }
            // 进行滑动
            page.setTranslationX(this.getWidth());
            // 将显示的界面左移
            ObjectAnimator animator = ObjectAnimator.ofFloat(showPage, "translationX", 0f,-this.getWidth());
            ObjectAnimator animator1 = ObjectAnimator.ofFloat(page, "translationX", this.getWidth(),0);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.setDuration(1000);
            animatorSet.playTogether( animator, animator1);
            animatorSet.start();
            // 滑动完成后数据更换
            View p = showPage;
            showPage = page;
            page = p;
            // 当前显示的页码
            ++index;
            // 已经展示过的数据，不包含当前展示的
            showSize += text.getCharNum();
            sizes.add(text.getCharNum());
            text = showPage.findViewById(R.id.read_text_view);
            text.setText(str.substring(showSize));
        }
        if(e2.getX()-e1.getX()>50){
            if(index == 1){
                Toast.makeText(getContext(),"第一页",Toast.LENGTH_SHORT).show();
                return false;
            }
            // 将未显示的界面放置左侧
            page.setTranslationX(-this.getWidth());
            // 左侧的移动到主界面
            ObjectAnimator animator = ObjectAnimator.ofFloat(page, "translationX", -this.getWidth(),0f);
            ObjectAnimator animator1 = ObjectAnimator.ofFloat(showPage, "translationX", 0,this.getWidth());
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.setDuration(1000);
            animatorSet.playTogether( animator, animator1);
            animatorSet.start();

            showSize -= sizes.get(sizes.size()-1);
            sizes.remove(sizes.size()-1);
            --index;

            View p = showPage;
            showPage = page;
            page = p;

            ReadTextView text = showPage.findViewById(R.id.read_text_view);
            text.setText(str.substring(showSize));
        }
        return false;
    }
}
